﻿Imports System.Data.OleDb
''' <summary>
''' 行业科目
''' 对应表-S_IndustryAccount
''' </summary>
Public Class ClsIndustryAccountDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_INDUSTRY_ACCOUNT)
    End Sub
    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    ''' <summary>
    ''' 返回指定行业编号 和 科目编号 的科目对象
    ''' </summary>
    ''' <param name="industryId"></param>
    ''' <param name="acct"></param>
    ''' <returns></returns>
    Public Function GetAcctByIdCode(ByVal industryId As Integer, ByVal acct As String) As ClsIndustryAccount
        Dim tmpCls As ClsIndustryAccount = Nothing

        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()
            Dim tmpList As List(Of ClsIndustryAccount)
            Dim tmpParas(1) As OleDb.OleDbParameter

            '打开数据库
            tmpDbAccess.DbOpen()

            '构造查询语句
            tmpSql = "select * from [" & TABLE_NAME_INDUSTRY_ACCOUNT & "] "
            tmpSql &= "where [IndustryId]=@IndustryId "
            tmpSql &= " and  [AcctCode]=@AcctCode "

            tmpParas(0) = New OleDbParameter("", OleDb.OleDbType.Integer)
            tmpParas(1) = New OleDbParameter("", OleDb.OleDbType.VarWChar)
            tmpParas(0).Value = industryId
            tmpParas(1).Value = acct

            '查询并将返回数据解析为List对象
            tmpList = TryCast(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql, tmpParas)), List(Of ClsIndustryAccount))

            If tmpList.Count > 0 Then
                tmpCls = tmpList.Item(0)
            End If

            '关闭数据库
            tmpDbAccess.DbClose()


        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsIndustryAccountDbAccess - GetAllAcctByIndustryId"

        End Try

        Return tmpCls

    End Function

    ''' <summary>
    ''' 取出所选行业的所有科目
    ''' </summary>
    ''' <param name="industryId"></param>
    ''' <returns></returns>
    Public Function GetAllAcctByIndustryId(ByVal industryId As Int16) As List(Of ClsIndustryAccount)
        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection() 'New ClsDbAccessMdb(mConnectionString)
            Dim tmpList As List(Of ClsIndustryAccount)
            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "select * from [" & TABLE_NAME_INDUSTRY_ACCOUNT & "] where [IndustryId]=" & industryId

            '查询并将返回数据解析为List对象
            tmpList = TryCast(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql)), List(Of ClsIndustryAccount))

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpList

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsIndustryAccountDbAccess - GetAllAcctByIndustryId"

            Return Nothing
        End Try

    End Function

    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object 'List(Of ClsIndustryAccount)
        If reader Is Nothing Then
            Return Nothing
        End If
        Try

            Dim tmpList As List(Of ClsIndustryAccount) = New List(Of ClsIndustryAccount)
            Dim tmpCls As ClsIndustryAccount

            While reader.Read()

                tmpCls = New ClsIndustryAccount()
                'InnerId,IndustryId,AcctLevel,ParentAcctCode,AcctCode,AcctName,AcctType,DebitCredit,IfLastAcct
                With tmpCls
                    .InnerId = reader.GetInt32(0)
                    .IndustryId = reader.GetInt16(1)
                    .AcctLevel = reader.GetInt16(2)
                    .ParentAcctCode = ConvertDbNullString(reader(3))
                    .AcctCode = ConvertDbNullString(reader(4))
                    .AcctName = ConvertDbNullString(reader(5))
                    .AcctType = ConvertDbNullString(reader(6))
                    .DebitCredit = reader.GetInt32(7) '-1 贷方 1 借方
                    .IfLastAcct = reader.GetBoolean(8)

                End With

                tmpList.Add(tmpCls)

            End While

            Return tmpList

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsIndustryAccountDbAccess - ConvertRecordToClass"

            Return Nothing
        End Try
    End Function
End Class
